package com.anonymous.community.ui.dashboard;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import com.anonymous.community.databinding.FragmentDashboardBinding;
import com.anonymous.community.ui.base.BaseFragment;

public class DashboardFragment extends BaseFragment {

    private FragmentDashboardBinding binding;
    private long lastBackPressedTime = 0L;
    private static final int EXIT_INTERVAL_MS = 2000;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // 设置返回按钮点击事件
        if (binding.btnBack != null) {
            Log.d("DashboardFragment", "返回按钮找到，设置点击监听器");
            binding.btnBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("DashboardFragment", "返回按钮被点击");
                    // 使用多种方法确保返回功能正常工作
                    try {
                        // 方法1: 使用 FragmentManager 的 popBackStack
                        if (getParentFragmentManager().getBackStackEntryCount() > 0) {
                            getParentFragmentManager().popBackStack();
                            Log.d("DashboardFragment", "使用 FragmentManager popBackStack 返回成功");
                            return;
                        }
                        
                        // 方法2: 尝试使用 Navigation 组件
                        Navigation.findNavController(v).navigateUp();
                        Log.d("DashboardFragment", "使用 Navigation 返回成功");
                    } catch (Exception e) {
                        Log.d("DashboardFragment", "Navigation 返回失败: " + e.getMessage());
                        // 方法3: 使用 Activity 的 onBackPressed
                        if (getActivity() != null) {
                            getActivity().onBackPressed();
                            Log.d("DashboardFragment", "使用 Activity onBackPressed 返回");
                        }
                    }
                }
            });
        } else {
            Log.e("DashboardFragment", "错误：无法找到返回按钮 btnBack");
        }
        
        // 设置系统返回键二次退出
        setupDoubleBackExit();
        
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    /**
     * 设置系统返回键二次退出功能
     * 在发布页面中，两秒内连续按两次返回键退出应用
     */
    private void setupDoubleBackExit() {
        requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                long now = System.currentTimeMillis();
                if (now - lastBackPressedTime < EXIT_INTERVAL_MS) {
                    // 二次返回：退出应用
                    requireActivity().finishAffinity();
                } else {
                    lastBackPressedTime = now;
                    Toast.makeText(getContext(), "再按一次退出应用", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected String getPageName() {
        return "发现页";
    }
}